import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15
import QtQuick.Layouts 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 30
    property bool pwdTrue:false
    Rectangle{
        width:800
        height: 120
        color:"transparent"
            GridLayout{
                id:changePwdLayout
                anchors.centerIn: parent
                columns: 4
                rows:3

                Button
                {
                    Layout.row:0
                    Layout.column:0
                    Layout.preferredWidth: 180
                    Layout.preferredHeight: btnHeight
                    // x: 152
                    // y: 13
                    text: qsTr("原密码")
                    font.pixelSize: 20
                    background: Rectangle{
                        color: btnColor
                            }
                    onClicked: {
                        miniKeyBoard.lineValue = "";
                        miniKeyBoard.textInput=oldPwd;
                    }
                }

                Rectangle {
                    id: recOldPwd
                    width:300
                    height:30
                    // x: 218
                    // y: 7
                    color: "transparent"
                    border.width: 0
                    border.color: "white"
                    Layout.row:0
                    Layout.column:1
                    //Layout.preferredWidth: buttonWidth
                    Layout.preferredHeight: 25
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "white"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: recOldPwd.height
                            PathLine {
                                x: recOldPwd.width
                                y: recOldPwd.height
                            }
                        }
                    }
                    TextInput{
                        id:oldPwd
                        anchors.fill: parent
                        echoMode: TextInput.Password
                        color:"white"
                        font.pixelSize: 25
                        horizontalAlignment: Text.AlignHCenter
                        MouseArea {
                               anchors.fill: parent
                               onClicked: {
                                   miniKeyBoard.lineValue = "";
                                   miniKeyBoard.textInput=oldPwd;
                               }
                           }

                    }
                }
                Button
                {
                    // x: 151
                    // y: 51
                    Layout.row:1
                    Layout.column:0
                    Layout.preferredWidth: 180
                    Layout.preferredHeight: btnHeight
                    text: qsTr("新密码")
                    font.pixelSize: 20
                    background: Rectangle{
                        color: btnColor
                            }
                    onClicked: {
                        miniKeyBoard.lineValue = "";
                        miniKeyBoard.textInput=newPwd;
                    }
                }
                Rectangle {
                    Layout.row:1
                    Layout.column:1
                    //Layout.preferredWidth: buttonWidth
                    Layout.preferredHeight: 25
                    id: recNewPwd
                    width:300
                    height:30
                    x: 217
                    y: 44
                    color: "transparent"
                    border.width: 0
                    border.color: "white"
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "white"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: recNewPwd.height
                            PathLine {
                                x: recNewPwd.width
                                y: recNewPwd.height
                            }
                        }
                    }
                    TextInput{
                        id:newPwd
                        color:"white"
                        anchors.fill: parent
                        echoMode: TextInput.Password
                        font.pixelSize: 25
                        horizontalAlignment: Text.AlignHCenter
                        MouseArea {
                               anchors.fill: parent
                               onClicked: {
                                   miniKeyBoard.lineValue = "";
                                   miniKeyBoard.textInput=newPwd;
                               }
                           }
                    }
                }

                Button
                {
                    // x: 131
                    // y: 87
                    Layout.row:2
                    Layout.column:0
                    Layout.preferredWidth: 180
                    Layout.preferredHeight: btnHeight
                    text: qsTr("确认密码")
                    font.pixelSize: 20
                    background: Rectangle{
                        color: btnColor
                            }
                    onClicked: {
                        miniKeyBoard.lineValue = "";
                        miniKeyBoard.textInput=confirmPwd;
                    }
                }
                Rectangle {
                    id:recConfirmPwd
                    // x: 217
                    // y: 84
                    Layout.row:2
                    Layout.column:1
                    //Layout.preferredWidth: buttonWidth
                    Layout.preferredHeight: 25
                    width:300
                    height:30
                    color: "transparent"
                    border.width: 0
                    border.color: "white"
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "white"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: recConfirmPwd.height
                            PathLine {
                                x: recConfirmPwd.width
                                y: recConfirmPwd.height
                            }
                        }
                    }
                    TextInput{
                        anchors.fill: parent
                        id:confirmPwd
                        color: "white"
                        echoMode: TextInput.Password
                        font.pixelSize: 25
                        horizontalAlignment: Text.AlignHCenter
                        MouseArea {
                               anchors.fill: parent
                               onClicked: {
                                   miniKeyBoard.lineValue = "";
                                   miniKeyBoard.textInput=confirmPwd;
                               }
                           }
                    }
                }
                Button
                {
                    id:btnOK
                    //x: 675
                    //y: 20
                    Layout.row:0
                    Layout.column:2
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 100
                    Layout.rowSpan: 3
                    //width: 100
                    //height: 100
                    background: Rectangle{
                        border.width: 0
                        color: "#C070B0"
                            }

                    text: qsTr("确定")
                    font.pixelSize: 25
                    palette.buttonText: "white"
                    //Connections {
                    //    target: btnOK
                        onClicked: {
                            pwdTrue=false;
                            if(oldPwd.text.length === 0)
                            {
                                savePwdDialog.title=qsTr("请输入原密码！")
                                savePwdDialog.open()
                                return
                            }
                            if(newPwd.text.length === 0){
                                savePwdDialog.title=qsTr("请输入新密码！")
                                savePwdDialog.open()
                                return
                            }
                            if(confirmPwd.text.length === 0){
                                savePwdDialog.title=qsTr("请输入确认密码！")
                                savePwdDialog.open()
                                return
                            }
                            if(newPwd.text != confirmPwd.text){
                                savePwdDialog.title=qsTr("两次输入密码不一致！")
                                savePwdDialog.open()
                                return
                            }
                            if(mytables.verifyPwd(oldPwd.text))
                            {
                                if(mytables.changePwd(newPwd.text))
                                {

                                }
                                else{
                                    savePwdDialog.title=qsTr("保存失败！")
                                    savePwdDialog.open()
                                    return
                                }
                            }
                            else
                            {
                                savePwdDialog.title=qsTr("原密码不正确！")
                                savePwdDialog.open()
                                return
                            }
                            pwdTrue=true
                            savePwdDialog.title=qsTr("修改密码成功！")
                            savePwdDialog.open()

                        }
                    //}
                }
                Button
                {
                    id:btnReturn
                    //x: 675
                    //y: 20
                    Layout.row:0
                    Layout.column:3
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 100
                    Layout.rowSpan: 3
                    //width: 100
                    //height: 100
                    background: Rectangle{
                        border.width: 0
                        color: "#B0B0D0"
                            }

                    text: qsTr("返回")
                    font.pixelSize: 25
                    palette.buttonText: "white"
                    //Connections {
                    //    target: btnReturn
                        onClicked: {
                            //rectangle.selectedView = 2
                            bottomBar.selected =2
//                            myLoader.visible=false;
//                            myLoader.visible=true
                            myLoader.source="FunctionPanel.qml"
                        }
                    //}
                }
            }
    }
    MiniKeyBoard
    {
        id: miniKeyBoard
        width: root.width
        x: 34
        y: 126
        isNumber: false;
        textInput: oldPwd
    }
    Dialog {
        anchors.centerIn: parent
        closePolicy: Popup.CloseOnEscape
        id: savePwdDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        //title:"保存成功！"
        onAccepted: {
            savePwdDialog.close()
            if(pwdTrue){
                bottomBar.selected =0
//                myLoader.visible=false;
//                myLoader.visible=true;
                myLoader.source="StatusView.qml"
                //statusView.visible=true
            }
        }
        z:100
        //onRejected: console.log("Cancel clicked")
    }
}
